package com.hbb20;

import com.hbb20.utils.SystemUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.StackLayout;
import ohos.app.Context;

public class KeyboardView extends StackLayout implements Component.LayoutRefreshedListener {

    private int componentHeight = 0;
    private boolean showKeyboard = false;

    public KeyboardView(Context context) {
        this(context, null);
    }

    public KeyboardView(Context context, AttrSet attrSet) {
        this(context, attrSet, "");
    }

    public KeyboardView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView();
    }

    private void initView() {
        setLayoutRefreshedListener(this);
    }


    @Override
    public void onRefreshed(Component component) {
        int currentHeight = component.getHeight();
        this.componentHeight = Math.max(currentHeight, componentHeight);
        if (showKeyboard) {
            if (componentHeight == currentHeight) {
                this.showKeyboard = false;
                KeyboardManager.get().dispatchListener(this.showKeyboard, 0);
            }
        } else {
            int height = this.componentHeight - currentHeight;
            if (height >= SystemUtils.vp2px(getContext(), 200)) {
                this.showKeyboard = true;
                KeyboardManager.get().dispatchListener(this.showKeyboard, height);
            }
        }
    }
}
